﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.CRL.DataContract;

namespace Empires.PL.PageBuilder
{
    public static class ControlBuilder
    {
        /// <summary>
        /// 输出分页页码。
        /// </summary>
        /// <param name="currentpage">当前页码</param>
        /// <param name="pageitems">每页多少条记录</param>
        /// <param name="itemcount">一共多少条记录</param>
        /// <param name="formatlink">如：/Home/Blog/{0} 0=>pageindex</param>
        /// <returns></returns>
        public static string BuildPagination(int currentpage, int pageitems, int itemcount,string formatlink)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"pagination\">");
            sb.Append("<ul>");
            int totalpage = itemcount % pageitems == 0 ? itemcount / pageitems : itemcount / pageitems + 1;
            if (totalpage < 9) //页面总数小于9。直接输出
            {
                for (int i = 0; i < totalpage; i++)
                {
                    sb.Append("<li><a href=\"");
                    sb.Append(String.Format(formatlink, i));
                    sb.Append("\"");
                    if (currentpage == i)
                    {
                        sb.Append(" class=\"active\"");
                    }
                    sb.Append(">"); //<li><a href="#">
                    sb.Append(i + 1);
                    sb.Append("</a></li>");
                }
            }
            else
            {
                if (currentpage < 3)
                {
                    for (int i = 0; i <= currentpage+2; i++) //先输出当前页+2本身及之前的页面
                    {
                        sb.Append("<li><a href=\"");
                        sb.Append(String.Format(formatlink, i));
                        sb.Append("\"");
                        if (currentpage == i)
                        {
                            sb.Append(" class=\"active\"");
                        }
                        sb.Append(">"); //<li><a href="#">
                        sb.Append(i + 1);
                        sb.Append("</a></li>");
                    }
                    sb.Append("<li>. . . . . .</li>");

                    //输出最后一页
                    sb.Append("<li><a href=\"");
                    sb.Append(String.Format(formatlink, totalpage - 1));
                    sb.Append("\"");
                    sb.Append(">"); //<li><a href="#">
                    sb.Append(totalpage);
                    sb.Append("</a></li>");
                }
                else if (currentpage > totalpage - 4) //最后3也
                {
                    for (int i = 0; i < 3; i++) //输出前3页
                    {
                        sb.Append("<li><a href=\"");
                        sb.Append(String.Format(formatlink, i));
                        sb.Append("\"");
                        sb.Append(">"); //<li><a href="#">
                        sb.Append(i + 1);
                        sb.Append("</a></li>");
                    }
                    sb.Append("<li>. . . . . .</li>");
                    for (int i = totalpage - 4; i < totalpage; i++) //输出最后3页
                    {
                        sb.Append("<li><a href=\"");
                        sb.Append(String.Format(formatlink, i));
                        sb.Append("\"");
                        if (currentpage == i)
                        {
                            sb.Append(" class=\"active\"");
                        }
                        sb.Append(">"); //<li><a href="#">
                        sb.Append(i + 1);
                        sb.Append("</a></li>");
                    }
                }
                else  //落在中间
                {
                    //输出第一页
                    sb.Append("<li><a href=\"");
                    sb.Append(String.Format(formatlink, 0));
                    sb.Append("\"");
                    sb.Append(">"); //<li><a href="#">
                    sb.Append(1);
                    sb.Append("</a></li>");

                    sb.Append("<li>. . . . . .</li>");
                    //输出中间3页
                    for (int i = currentpage - 1; i < currentpage + 2; i++)
                    {
                        sb.Append("<li><a href=\"");
                        sb.Append(String.Format(formatlink, i));
                        sb.Append("\"");
                        if (currentpage == i)
                        {
                            sb.Append(" class=\"active\"");
                        }
                        sb.Append(">"); //<li><a href="#">
                        sb.Append(i + 1);
                        sb.Append("</a></li>");
                    }
                    sb.Append("<li>. . . . . .</li>");
                    //输出最后一页
                    sb.Append("<li><a href=\"");
                    sb.Append(String.Format(formatlink, totalpage-1));
                    sb.Append("\"");
                    sb.Append(">"); //<li><a href="#">
                    sb.Append(totalpage);
                    sb.Append("</a></li>");
                }
            }
            sb.Append("</ul>");
            sb.Append("<div class=\"cl\">&nbsp;</div>");
            sb.Append("</div>");
            return sb.ToString();
            //<div class="pagination">
            //    <ul>
            //        <li><a href="#">1</a></li>
            //        <li><a href="#" class="active">2</a></li>
            //        <li><a href="#">3</a></li>
            //        <li><a href="#">4</a></li>
            //        <li><a href="#">5</a></li>
            //        <li><a href="#">6</a></li>
            //        <li><a href="#">&raquo;</a></li>
            //    </ul>
            //    <div class="cl">&nbsp;</div>
            //</div>
        }

        /// <summary>
        /// 输出首先滚动图片配置相关代码
        /// </summary>
        /// <returns></returns>
        public static MainInfoDisplayPack BuildMianInfoDisplaySegment<T>(T model, int MIID) where T : BaseModel
        {
            MainInfoDisplayPack result = new MainInfoDisplayPack();
            //result.Script = "<script type=\"text/javascript\" src=\"/Scripts/fileuploader.js\"></script><link rel=\"Stylesheet\" type=\"text/css\" href=\"/Content/css/fileuploader.css\" />  <script type=\"text/javascript\">$(function () {var uploader = new qq.FileUploader({element: document.getElementById('uploadbox'), action: '/Upload', debug: false, onSubmit: function () {$('#loadbox').append($('<img src=\"/Content/images/Loading.gif\" style=\"width:30px;height:30px;margin-left:30px;\" />'));},onComplete: function (id, file, response) {$('#loadbox').children('img').remove();var uri = response.msg;$('#picturecontrol').attr('src', uri);$('#hidinfophoto').val(uri);$('#picturebox').show(); }});});function MainDisplay(sender) { if ($(sender).attr('checked')) {$('#mainbox').show();}else { $('#mainbox').hide(); $('#picturebox').hide();$('#picturecontrol').attr('src', ''); $('#txtMainTitle').val('');}}</script>";
            //StringBuilder sb = new StringBuilder();
            //sb.Append("<tr height=\"30\"> <td>&nbsp;</td><td>是否首页展示：</td><td><input type=\"checkbox\" name=\"CheckMain\"");
            //if(model.IsMain)
            //{
            //    sb.Append(" checked=\"checked\" ");
            //}
            //sb.Append("id=\"ckbMain\" onchange=\"MainDisplay(this)\" />");
            //sb.Append("<input type=\"hidden\" name=\"INFOID\" ");
            //if(model.IsMain)
            //{
            //    sb.Append("value=\""+model.INFOID+"\"");
            //}
            //else
            //{
            //    sb.Append("value=\"-1\"");
            //}
            //sb.Append("<input type=\"hidden\" name=\"MIID\" value=\"");
            //sb.Append(MIID);
            //sb.Append("\" />");
            //sb.Append("<label for=\"ckbMain\">首页展示</label> </td><td>如果选择首页展示，您需要选择能代表此条信息主题图片和图片描述</td></tr> <tbody id=\"mainbox\"  ");
            //if(!model.IsMain)
            //{
            //    sb.Append(" style=\"display:none;\" ");
            //}
            //sb.Append("><tr height=\"30\"><td>&nbsp;</td><td>主题图片：</td><td><div style=\"float:left;\" id=\"uploadbox\"></div><div id=\"loadbox\"></div></td><td>请选能代表该条信息的主题主题图片,如果不选择新图片则表示不改变当前图片</td></tr><tr id=\"picturebox\" style=\"display:none;\"><td>&nbsp;</td> <td colspan=\"3\"><input type=\"hidden\" id=\"hidinfophoto\" name=\"INFOPHOTO\" value=\"");
            //sb.Append(model.INFOPHOTO);
            //sb.Append("\" /><img id=\"picturecontrol\" style=\"width:700px; height:250px;\"/></td></tr><tr height=\"30\"><td>&nbsp;</td><td>主题图片描述</td><td><input type=\"text\" id=\"txtMainTitle\" name=\"PHOTOTITLE\" value=\"");
            //sb.Append(model.PHOTOTITLE);
            //sb.Append("\" style=\"width:300px;\"/></td> <td>请描述该图片，如，该条主题图片所反映的信息是什么样的</td></tr> </tbody>");
            //result.Html = sb.ToString();
            return result;
        }
    }
    public class MainInfoDisplayPack
    {
        public string Script { get; set; }
        public string Html { get; set; }
    }
}
